//
//  STOrderStatisticsView.m
//  YiYueStore
//
//  Created by zhouzhouzhou on 2018/10/16.
//  Copyright © 2018年 YuYiLian. All rights reserved.
//

#import "STOrderStatisticsView.h"
#import "STDaySelCell.h"
#import "STStatisticsCell.h"
#import "UIButton+ZYHblock.h"

@implementation STOrderStatisticsView

-(void)setOrderStatisticsListView{
     arrRows = @[@[@"待核销订单",@"总订单",@"总收入",@"总业绩"],];
    _listData = [NSArray new];
    [self loadOrderStatisticsView];
}


-(void)reloadTimerOrType{
    [statisticsTableView reloadRow:0 inSection:1 withRowAnimation:UITableViewRowAnimationNone];
}


-(void)reloadOrderCountView{
    [statisticsTableView reloadSection:0 withRowAnimation:UITableViewRowAnimationNone];
}

-(void)reloadOrderListView{
    [statisticsTableView reloadSection:1 withRowAnimation:UITableViewRowAnimationNone];
}


- (void)loadOrderStatisticsView {
    if (!statisticsTableView) {
        statisticsTableView = [[UITableView alloc]initWithFrame:self.bounds style:UITableViewStylePlain];
        statisticsTableView.delegate = self;
        statisticsTableView.dataSource = self;
        statisticsTableView.backgroundColor = [UIColor clearColor];
        statisticsTableView.separatorInset = UIEdgeInsetsZero;
        statisticsTableView.tableFooterView = [UIView new];
        [self addSubview:statisticsTableView];
    }else{
        [statisticsTableView reloadData];
    }
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section ==0) {
        NSArray * arr = arrRows[section];
        return arr.count;
    }else{
        return _listData.count+2;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section ==1 && indexPath.row==0) {
        return 44;
    }
    return 44;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 1) {
        return 0;
    }
    return 10;
}


- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView * view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    return view;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString * identifier = @"commonCell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
    }
    if (indexPath.section ==0) {
        cell.accessoryType =UITableViewCellAccessoryNone;
        NSArray * arr = arrRows[indexPath.section];
        cell.textLabel.text = arr[indexPath.row];
        cell.textLabel.font = FONT(15);
        cell.detailTextLabel.font = FONT(15);
        if (indexPath.row ==0) {
            cell.detailTextLabel.text =[NSString stringWithFormat:@"%d单",self.countModel.orderNum];
        }else if (indexPath.row ==1) {
            cell.detailTextLabel.text = [NSString stringWithFormat:@"%d单",self.countModel.verificationOrderNum];
        }else if (indexPath.row ==2) {
            cell.detailTextLabel.text = [NSString stringWithFormat:@"¥%0.2f",self.countModel.incomeMoney];
        }else if (indexPath.row ==3) {
            cell.detailTextLabel.text = [NSString stringWithFormat:@"¥%0.2f",self.countModel.achievementMoeny];
        }
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }else if (indexPath.section ==1){
        if (indexPath.row ==0){
            static NSString * identifier = @"STDaySelCell";
            STDaySelCell * cell =[tableView dequeueReusableCellWithIdentifier:identifier];
            if (!cell) {
                cell = [[STDaySelCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
            }
            [cell setDaySelWithInfo:@{}];
            [cell.fromBtn setTitle:self.starTime forState:UIControlStateNormal];
            [cell.endBtn setTitle:self.endTime forState:UIControlStateNormal];
            WeakSelf
            cell.fromBtn.didSelectBtn = ^(UIButton *sender) {
                [weakSelf actionChangeFormDay];
            };
            cell.endBtn.didSelectBtn = ^(UIButton *sender) {
                [weakSelf actionChangeEndDay];
            };
            cell.selectHairType = ^(NSInteger index) {
                [weakSelf actionSelectHairType:index];
            };
            cell.sureBtn.didSelectBtn = ^(UIButton *sender) {
                [weakSelf actionSureSelectCondition];
            };
            return cell;
        }else {
            static NSString * identifier = @"STStatisticsCell";
            STStatisticsCell * cell =[tableView dequeueReusableCellWithIdentifier:identifier];
            if (!cell) {
                cell = [[STStatisticsCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
            }
           
            if (indexPath.row ==1) {
                [cell setStatisticsWithDic:nil type:0];
            }else{
                STOrderSumCountSumModel * model = _listData[indexPath.row-2];
                cell.isSum = YES;
                [cell setStatisticsWithDic:model type:1];
            }
            return cell;
        }
    }
    
    return cell;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

- (void)actionChangeFormDay{
    if ([self.delegate respondsToSelector:@selector(orderStatisticsViewChangeDay:type:)]) {
        [self.delegate orderStatisticsViewChangeDay:@{@"data":self.starTime} type:@"from"];
    }
}

- (void)actionChangeEndDay{
    if ([self.delegate respondsToSelector:@selector(orderStatisticsViewChangeDay:type:)]) {
        [self.delegate orderStatisticsViewChangeDay:@{@"data":self.endTime} type:@"end"];
    }
}

-(void)actionSelectHairType:(NSInteger)index{
    if ([self.delegate respondsToSelector:@selector(orderStatisticsViewSelType:)]) {
        [self.delegate orderStatisticsViewSelType:index];
    }
}

-(void)actionSureSelectCondition{
    if ([self.delegate respondsToSelector:@selector(orderStatisticsViewSureSelectCondition)]) {
        [self.delegate orderStatisticsViewSureSelectCondition];
    }
}

-(void)reloadView{
    [statisticsTableView reloadData];
}
@end
